/**
 * 初始化消息推送详情对话框
 */
var PushMsgInfoDlg = {
    form: null
};


/**
 * 清除数据
 */
PushMsgInfoDlg.clearForm = function() {
    $("#pushForm")[0].reset();
}


/**
 * 获取商户列表
 */
PushMsgInfoDlg.getAppNameList = function(){
    var ajax = new $ax(Feng.ctxPath + "/application/appNameList", function (data) {
        data.forEach(function (item) {
            var appAlias = !$.trim(item.appAlias)?"":"("+item.appAlias+")";
            $("#appCode").append("'<option value='"+item.appCode+"'>"+item.appName+appAlias+"</option>'");
        }) ;
    }, function (data) {
        Feng.error("获取应用列表失败!");
    });
    ajax.start();
}

PushMsgInfoDlg.initForm = function(){
    layui.use('form', function(){
        var form = layui.form;
        PushMsgInfoDlg.form = form;
        var loading;

        //监听提交
        form.on('submit(formPushMsg)', function(data){
            if(data.field.appCode == "请选择"){
                Feng.error("请选择应用");
            }else{
                layer.confirm("是否想要立即推送信息",function (index) {
                    layer.close(index);
                    loading = layer.load(0,{
                        shade:false,
                        time: 30 * 1000
                    });
                    setTimeout(function () {
                        var ajax = new $ax(Feng.ctxPath + "/pushMsg/sendMsg", function (data) {
                            layer.closeAll();
                            Feng.success("推送成功!");
                            PushMsgInfoDlg.clearForm();
                        }, function (data) {
                            layer.closeAll();
                            Feng.error("推送失败!" + data.responseJSON.message + "!");
                        });
                        ajax.set(data.field);
                        ajax.start();
                    },300)
                });
            }

            return false;
        });
    });
}

$(function() {
    PushMsgInfoDlg.getAppNameList();
    PushMsgInfoDlg.initForm();
});
